import os

from twisted.protocols import basic
from twisted.internet import reactor

from toneedu.parser import Parser

class CmdLine(basic.LineReceiver):
    """
    Protocol which implements the basic command-line user interface
    """

    delimiter = os.linesep
    
    def prompt(self):
        self.transport.write(self.parser.getPrompt())

    def connectionMade(self):
        self.parser = Parser()

        self.sendLine("Welcome to ToneEdu!")
        self.prompt()

    def lineReceived(self, line):
        self.issueCommand(line)
        self.prompt()

    def issueCommand(self, command):
        response = self.parser.issueCommand(command)
        if response is not None:
            self.sendLine(response)

